package com.beiyou.orderingsystemonweb.shop.service;

import com.beiyou.orderingsystemonweb.shop.dao.ShopDao;
import com.beiyou.orderingsystemonweb.shop.dao.ShopFoodDao;
import com.beiyou.orderingsystemonweb.shop.domain.Shop;
import com.beiyou.orderingsystemonweb.shop.domain.ShopFood;
import com.beiyou.orderingsystemonweb.shop.domain.UserAddress;
import com.beiyou.orderingsystemonweb.system.dao.CodeDao;
import com.beiyou.orderingsystemonweb.user.dao.UserDao;
import javafx.scene.input.DataFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 特殊的shopControl的方法
 */
@Component
public class ShopService {
    @Autowired
    ShopDao shopDao;
    @Autowired
    UserDao userDao;
    //ajax请求，
    //返回值是List
    public List queryShopByPos(String lng,String lat,Integer from,Integer size){
        //可以判断一下用户送餐地址的经纬度
        return shopDao.queryMapByUserPos(lng,lat,from,size);
    }

    public Integer addInterset(String sid, String uid){
        //查询用户是否存在
        if (userDao.queryUserByUid(uid)!=null){
            //查询店铺是否存在
            if (shopDao.queryShopCountBySid(sid)>0){
                //查询是否已经关注
                if (shopDao.queryInterestShopBySid(sid,uid)>0){
                    return null;
                }
                return userDao.addInterset(UUID.randomUUID().toString().replace("-",""),sid,uid);
            }
        }
        return null;
    }
    public UserAddress queryUserAddressByUid(String uid){
        return userDao.queryUserAddressByUid(uid);
    }

    public void saveShopLoginHistory(String uid,String key,String pos){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");
        //查询用户是否存在,
        int i = shopDao.queryShopCountBySid(uid);
        if (i!=1){
            return;
        }
        String id = UUID.randomUUID().toString().replace("-","");
        if (pos==null){
            pos = "河南省郑州市";
        }

        shopDao.saveShopLoginHistory(id,uid,pos,key,sdf.format(new Date()));
    }

    public String queryLoginMsg(String sid) {
        Map map = shopDao.queryLastLoginKey(sid);
        if (map!=null){

            return "最后一次登录时间："+map.get("loginTime")+",登录关键字:"+map.get("loginKey")+",登录地点:"+map.get("loginPos");
        }
        return null;
    }

    public Map shopInfo(String sid){
        Shop shop = shopDao.queryShopBySid(sid);
        if (shop==null){
            return null;
        }
        Map shopMap = new HashMap();
        shopMap.put("shopName",shop.getShopName());
        shopMap.put("shopImg",shop.getShopImg());
        shopMap.put("shopLoc",shop.getShopLoc());
        List<Map> hotFoodList = shopDao.queryHotFood4Shop(sid);
        //恰好就是我要的
        shopMap.put("hotFoodList",hotFoodList);
        return shopMap;
    }

    public Map<String,Object> shopFoodList(String sid,Integer begin, Integer size){
        List<Map> foodList = userDao.queryInterestShopBySid(sid, begin, size);

        Map<String,Object> shopFoodMap = new HashMap();
        if (foodList.size()!=1||foodList.get(0).get("foodId")!=null){
            shopFoodMap.put("foodNum",foodList.size());
        }
        else{
            shopFoodMap.put("foodNum",0);
            return shopFoodMap;
        }
        List<Map<String,Object>> fooList2 = new ArrayList<>();
        shopFoodMap.put("foodList",fooList2);
        foodList.forEach(food->{
            Map<String,Object> map = new HashMap<>();
            map.put("foodId",food.get("foodId"));
            map.put("foodImg",food.get("foodImg"));
            map.put("foodPrice",food.get("foodPrice"));
            map.put("foodName",food.get("foodName"));
            map.put("foodSale",food.get("foodSale"));
            map.put("foodReport",food.get("foodReport"));
            fooList2.add(map);
        });
        return shopFoodMap;
    }
    public Map<String,Object> shopFoodList(String sid,Integer begin, Integer size,Integer type){
        List<Map> foodList = userDao.queryInterestShopBySidToType(sid, begin, size,type.toString());

        Map<String,Object> shopFoodMap = new HashMap();
        if (foodList.size()!=1||foodList.get(0).get("foodId")!=null){
            shopFoodMap.put("foodNum",foodList.size());
        }
        else{
            shopFoodMap.put("foodNum",0);
            return shopFoodMap;
        }
        List<Map<String,Object>> fooList2 = new ArrayList<>();
        shopFoodMap.put("foodList",fooList2);
        foodList.forEach(food->{
            Map<String,Object> map = new HashMap<>();
            map.put("foodId",food.get("foodId"));
            map.put("foodImg",food.get("foodImg"));
            map.put("foodPrice",food.get("foodPrice"));
            map.put("foodName",food.get("foodName"));
            map.put("foodSale",food.get("foodSale"));
            map.put("foodReport",food.get("foodReport"));
            fooList2.add(map);
        });
        return shopFoodMap;
    }

    public Map<String,Object> shopFoodManage(String sid){
        Map<String, Object> shopMap = new HashMap<>();
//      foodNameList
        List<Map> foodNameList =  shopFoodDao.queryAllFoodBySid(sid);
        shopMap.put("foodNameList",foodNameList);


        //这个就是系统管理，本来是树的结构
        List<Map<String,Object>> ulList = new ArrayList<>();
        //查询所有菜品管理下的codekey
        codeDao.queryValue("菜品管理","菜品管理").forEach(ulvalue->{
            Map<String,Object> ulMap = new HashMap<>();
            ulMap.put("valueName",ulvalue);
            List<Map<String,Object>> liList = new ArrayList<>();
            ulMap.put("liList",liList);
            codeDao.queryValue(ulvalue,"菜品管理").forEach(livalue->{
                Map<String,Object> liMap = new HashMap<>();
                liMap.put("valueName",livalue);
                liList.add(liMap);
            });
            ulList.add(ulMap);
        });
        shopMap.put("ulList",ulList);

        return shopMap;
    }
    @Autowired
    ShopFoodDao shopFoodDao;
    @Autowired
    CodeDao codeDao;

    public Map<String,Object> queryShopInfo(String sid){
        Map map = shopDao.queryShopInfoBySid(sid);
        if (map!=null){
            map.put("WorkType",Integer.parseInt(map.get("WorkType").toString()));
        }
        return map;
    }
}
